﻿using System;
using System.Collections;
using System.Web.UI;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Entities;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;


namespace CRS.Web.BackStageManagement
{
    public partial class UserGroupUpdate : VcPage
    {
        readonly CastleAccess acc = new CastleAccess();
        BaseDataManager help = new BaseDataManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write(
                "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            if (!IsPostBack)
            {
                labelId.Text = publ.GetUrlToReceive(Request.QueryString["id"].Trim());
                var opt = Request["opt"];
                if (opt == "Edit")
                {
                    if (!string.IsNullOrEmpty(labelId.Text.Trim()))
                    {
                        var objMenu = new ContactInfo();
                        IList lstObj = acc.GetObjectList(new ContactInfo().GetType().Name, "Id", labelId.Text.Trim());
                        if (lstObj != null && lstObj.Count > 0)
                        {
                            objMenu = (ContactInfo)(lstObj[0]);

                            ZLTextBox_UserGroupName.Text = objMenu.Name;
                            txtAddress.Text = objMenu.Address;
                            txtMemo.Text = objMenu.Des;
                            txtPhone.Text =objMenu.PhoneNumber;
                            txtResponsible.Text = objMenu.Responsible;
                            txtCoordinate.Text = objMenu.Coordinate;
                        }
                    }
                }
            }
        }



        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserGroupList.aspx", false);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var opt = Request["opt"];
            if (opt == "Edit")
            {
                var objUserGroup = new ContactInfo();
                var lstObj = acc.GetObjectList(new ContactInfo().GetType().Name, "Id", labelId.Text.Trim());
                if (lstObj != null && lstObj.Count > 0)
                {

                    var list =
                        help.CheckExist("from ContactInfo where Name='" + ZLTextBox_UserGroupName.Text + "' and Id != '" + labelId.Text.Trim() +
                                        "'");
                    if (list != null && list.Count > 0)
                    {
                        Alert.MessageBox("名称为: " + ZLTextBox_UserGroupName.Text + "  的商户已存在，请检测！！！");
                        return;
                    }
                    objUserGroup = (ContactInfo)(lstObj[0]);
                }

                objUserGroup.Name = ZLTextBox_UserGroupName.Text;
                objUserGroup.Address = txtAddress.Text;
                objUserGroup.Des = txtMemo.Text;
                objUserGroup.PhoneNumber = txtPhone.Text;
                objUserGroup.Responsible = txtResponsible.Text;
                objUserGroup.Coordinate = txtCoordinate.Text;

                var access = help.SaveOrUpdateObj(objUserGroup);

                if (access)
                    Alert.MessageBox("修改成功!");
                else
                    Alert.MessageBox("添加失败!");
            }
            else
            {

                var objUserGroup = new ContactInfo();

                var list =
                    help.CheckExist("from UserGroup where Name='" + ZLTextBox_UserGroupName.Text + "' and Id != '" +
                                    labelId.Text.Trim() +
                                    "'");
                if (list != null && list.Count > 0)
                {
                    Alert.MessageBox("名称为: " + ZLTextBox_UserGroupName.Text + "  的商户已存在，请检测！！！");
                    return;
                }
                objUserGroup.Name = ZLTextBox_UserGroupName.Text;
                objUserGroup.Address = txtAddress.Text;
                objUserGroup.Des = txtMemo.Text;
                objUserGroup.PhoneNumber = txtPhone.Text;
                objUserGroup.Responsible = txtResponsible.Text;
                objUserGroup.Coordinate = txtCoordinate.Text;
                var access = help.SaveOrUpdateObj(objUserGroup);

                if (access)
                    Alert.MessageBox("添加成功!");
                else
                    Alert.MessageBox("添加失败!");
            }
        }
    }
}
